/*
 * 1806. 买水果
 * 小明要帮公司买水果，给了一个codeList，里面装的是他买的水果，给了一个shoppingCart,里面装的是target水果，
 * 目标是检查codelist里的水果顺序是否和shoppingCart里的顺序匹配。注意的是只有codelist中的所有链表的item之后加起来小于等于shoppingcart里item之和才可能返回1，
 * 另外在codelist中有可能出现item时"anything"，它可以和任意的水果匹配。
 * 
 * 样例
 * 给出codeList = [["apple", "apple"],["orange", "banana", "orange"]]，shoppingCart = ["orange", "apple", "apple", "orange", "banana", "orange"]，返回1。
 * 解释：
 * 因为codelist 里的顺序和 shoppingcart 里除了第一个 orange 之后的水果匹配。
 * 
 * 给出codeList = [["orange", "banana", "orange"],["apple", "apple"]],shoppingCart = ["orange", "apple", "apple", "orange", "banana", "orange"],返回 0。
 * 解释：
 * 因为codeList里的顺序和shoppingcart没法匹配。
 * 
 * codeList = [["apple", "apple"],["orange", "anything", "orange"]],shoppingCart = ["orange", "apple", "apple", "orange", "mango", "orange"]，返回1。
 * 解释：
 * anything可以匹配mango，所以codelist可以匹配shoppingCart的水果。
 * 
 * 2018.06.13 @jeyming
 */
package buy_fruits_1806;

import java.util.ArrayList;
import java.util.List;

public class buy_fruits_1806 {
    /**
     * @param codeList: The codeList
     * @param shoppingCart: The shoppingCart
     * @return: The answer
     */
    public static int buyFruits(List<List<String>> codeList, List<String> shoppingCart) {
        // Write your code here
    	//int cntCodeList = 0;
    	int cntShoppingCart = 0;
    	for(int i = 0; i < codeList.size(); ++i) {
    		boolean judge = false;
    		for(int j = 0; j < codeList.get(i).size();) {
    			if(cntShoppingCart >= shoppingCart.size()) {
    				return 0;
    			}
    			if((shoppingCart.get(cntShoppingCart).equals(codeList.get(i).get(j)) != true)) {
    				if(codeList.get(i).get(j).equals("anything") == true) {
    					judge = true;
        				++j;
    				} else if(judge != false) {
    					return 0;
    				}
    			} else {
    				judge = true;
    				++j;
    			}  			
    			++cntShoppingCart;
    		}
    	}
    	return 1;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//[["apple","apple"],["orange","banana","orange"]]
		//["orange","apple","apple","orange","banana","orange"]
		List<List<String>> codeList = new ArrayList<List<String>>();
		List<String> list = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		List<String> shoppingCart = new ArrayList<String>();
		shoppingCart.add("orange");
		shoppingCart.add("apple");
		shoppingCart.add("apple");
		shoppingCart.add("orange");
		shoppingCart.add("banana");
		shoppingCart.add("orange");
		list.add("apple");
		list.add("apple");
		codeList.add(list);
		list2.add("orange");
		list2.add("banana");
		list2.add("anything");
		codeList.add(list2);
		System.out.println(buyFruits(codeList,shoppingCart));
		

	}

}
